home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Presentations / Presentations ’97 / Sessions ’97 / Multiplatform Code⁄Data Sharing / HelloBothWorlds / Shared / DemoNetAccess.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-26  |  2.9 KB  |  164 lines  |  [TEXT/CWIE]

  1. /*
  2.     DemoNetAccess.cpp
  3.     
  4.     Demo cross-platform web access class for MacHack 97
  5.     
  6.     Al Evans
  7.     
  8.     6/14/97
  9.     
  10. */
  11.  
  12. #include <string.h>
  13. #include "DemoNetAccess.h"
  14.  
  15. // Uses a "visible" public class to access a "hidden" implementation class
  16. // The implementation is completely different for the two platforms
  17.  
  18. #if TARGET_IS_MACOS
  19. // Mac implementation uses Internet Config
  20. #include "ICAPI.h"
  21. #include "ICKeys.h"
  22. #include "ICTypes.h"
  23. #endif
  24.  
  25. // Private implementation class
  26. class DemoWebImpl {
  27.  
  28.                 DemoWebImpl();
  29.             
  30.                 ~DemoWebImpl();
  31.             
  32. long            GoToWebSite(char const *url);
  33.  
  34. static void     ShowFailedAlert();
  35.                 
  36. friend struct DemoWebConnection;
  37.  
  38. #ifdef TARGET_IS_MACOS
  39.     ICInstance    icRef;
  40. #endif
  41. };
  42.  
  43. // There will be a single instance of the implementation class
  44. static DemoWebImpl *sWebImpl = nil;
  45.  
  46. #if 0
  47. #pragma mark ---------------------------------
  48. #pragma mark | Macintosh Implementation
  49. #pragma mark ---------------------------------
  50. #endif
  51.  
  52.  
  53. #if TARGET_IS_MACOS
  54. DemoWebImpl::DemoWebImpl()
  55. {
  56.     // Open Internet Config
  57.     ICError ret = ICStart(&icRef, 'DEMO');
  58.     
  59.     // Use default IC preferences
  60.     if (ret == noErr)
  61.         ret = ICFindConfigFile(icRef, 0, nil);
  62.     
  63.     // A real implementation should check for the presence and accessibility
  64.     // of a "preferred" web browser.
  65.     
  66.     if (ret != noErr)
  67.         throw(ret);
  68. }
  69.  
  70. DemoWebImpl::~DemoWebImpl()
  71. {
  72.     ICError ret = ICStop(icRef);
  73. }
  74.  
  75. long DemoWebImpl::GoToWebSite(char const *url)
  76. {
  77.     long start = 0;
  78.     long length = strlen(url);
  79.     return ICLaunchURL(icRef, "\phttp", (char *) url, length, &start, &length);
  80. }
  81.  
  82. void DemoWebImpl::ShowFailedAlert()
  83. {
  84.     // Not implemented in this demo
  85.     SysBeep(1);    
  86. }
  87. #endif
  88.  
  89. #if 0
  90. #pragma mark ---------------------------------
  91. #pragma mark | Windows 95 Implementation
  92. #pragma mark ---------------------------------
  93. #endif
  94.  
  95. #if TARGET_IS_WIN95
  96.  
  97. DemoWebImpl::DemoWebImpl()
  98. {
  99.     // A real implementation should check for the presence and accessibility
  100.     // of a "preferred" web browser.
  101. }
  102.  
  103.  
  104. DemoWebImpl::~DemoWebImpl()
  105. {
  106. }
  107.  
  108. long DemoWebImpl::GoToWebSite(char const *url)
  109. {
  110.     HINSTANCE browserInstance = ShellExecute(NULL, "open", (char *) url, 
  111.                 NULL, NULL, SW_SHOWNORMAL);
  112.     if ((long) browserInstance <= 32)
  113.         return ((long) browserInstance);
  114.     else
  115.         return 0;
  116.         
  117. }
  118.  
  119. void DemoWebImpl::ShowFailedAlert()
  120. {
  121.     // Not implemented in this demo
  122. }
  123.  
  124. #endif
  125.  
  126. #if 0
  127. #pragma mark ---------------------------------
  128. #pragma mark | Public Interface
  129. #pragma mark ---------------------------------
  130. #endif
  131.  
  132. DemoWebConnection::DemoWebConnection()
  133. {
  134.     if (sWebImpl == nil) {
  135.         try {
  136.             sWebImpl = new DemoWebImpl();
  137.         }
  138.         catch (...) {
  139.             sWebImpl = nil;
  140.         }
  141.     }
  142. }
  143.  
  144. DemoWebConnection::~DemoWebConnection()
  145. {
  146.     if (sWebImpl) delete sWebImpl;
  147.     sWebImpl = nil;
  148. }
  149.  
  150. Boolean DemoWebConnection::CanAccessWeb()
  151. {
  152.     return (sWebImpl != nil);
  153. }
  154.  
  155. long DemoWebConnection::OpenSite(char const *url)
  156. {    
  157.     long result = -1;
  158.     if (sWebImpl) 
  159.         result = sWebImpl->GoToWebSite(url);
  160.     if (result != 0)
  161.         DemoWebImpl::ShowFailedAlert();
  162.     return result;
  163. }
  164.